﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BossStateMachine : MonoBehaviour
{
    public int defautStateIndex;
    public BossState[] states;

    BossState currentState;

    void Awake()
    {
        foreach (var item in states)
        {
            item.gameObject.SetActive(false);
        }
        currentState = states[defautStateIndex];
        currentState.gameObject.SetActive(true);
        currentState.onEnter?.Invoke();
    }

    void Update()
    {
        foreach (var item in currentState.jumps)
        {
            if (item.condition.ToJump)
            {
                JumpState(item.target);
                break;
            }
        }
    }

    public void JumpState(int index)
    {
        JumpState(states[index]);
    }

    public void JumpState(BossState state)
    {
        foreach (var item in currentState.jumps)
        {
            item.condition.ToJump = false;
        }
        currentState.onExit?.Invoke();
        currentState.gameObject.SetActive(false);
        currentState = state;
        currentState.gameObject.SetActive(true);
        currentState.onEnter?.Invoke();
        Debug.Log($"jump to state \"{state.gameObject.name}\"");
    }
}
